//
//  MTMainViewController.m
//  YuchBox
//
//  Created by Ivan on 11-9-28.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MTMainViewController.h"
#import "UnitTEST.h"
#import "YuchBoxAppDelegate.h"

@implementation MTMainViewController

@synthesize testLable = _testLable;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSLog(@"mt main init");
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [_testLable release];
    [super dealloc];
}

- (void)setBarItems
{
    MTRootToolBarViewController *rootViewController =  [YuchBoxAppDelegate shareAppDelegate].rootToolBarViewController;
    
//    [rootViewController.navigationController.view setHidden:NO];
    [rootViewController.navigationItem setLeftBarButtonItem:nil];
    [rootViewController.navigationItem setTitle:@""];
}

- (void)clearBarItems
{
    MTRootToolBarViewController *rootViewController =  [YuchBoxAppDelegate shareAppDelegate].rootToolBarViewController;
    
//    [rootViewController.navigationController.view setHidden:YES];
    [rootViewController.navigationItem setLeftBarButtonItem:nil];
    [rootViewController.navigationItem setTitle:@""];
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"TEST@ viewDidLoad\n");
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{

}

- (void)viewDidDisappear:(BOOL)animated
{

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)setupLoginView:(id)sender
{
    [[YuchBoxAppDelegate shareAppDelegate] setSubViewTop:WindowViewTagForSpringboard];
//    NSLog(@"send keep alive");
//    [DATA_SOURCE sendKeepAlive];
}

#pragma mark -
#pragma IvanTabBarDelegate
- (void)viewWillAppearIvan:(BOOL)animated
{
    NSLog(@"#### will appearIvan");
    [self setBarItems];
}

- (void)viewDidAppearIvan:(BOOL)animated
{
    [[YuchBoxAppDelegate shareAppDelegate] setSubViewTop:WindowViewTagForSpringboard];
}

- (void)viewWillDisappearIvan:(BOOL)animated
{
    NSLog(@"#### did disappearIvan");
    [self clearBarItems];
}

- (NSInteger)showViewSequence
{
    return 0x2432;
}

#pragma mark - connect to the server and receive data
- (void)connectToServer
{
    //NSLog(@"HI! i am in connect");
    //[[IvanNetStream sharedIvanNetStream] connectWithServer:@"192.168.10.7" port:3333 delegate:self];
}

#pragma mark - IvanNetStreamDelegate method
- (void)getData:(NSData *)receivedData flag:(int)flag
{
    unsigned char * data = (unsigned char *)[receivedData bytes];
    for (int i = 0; i < 128; i++) {
        if (i%16 == 0)
            printf("\n");
        printf("%2x ", data[i]);
    }
    NSLog(@"HI! i am in");
    
   // NSString *tmpstr = [[NSString alloc] initWithFormat:@"receive msg: oh i get it %d"];
    
   // [_testLable setText:@"receive msg: oh i get a msg!"];
}

#pragma mark - TEST

- (IBAction)connect:(id)sender
{
    [DATA_SOURCE connect];
    //[IVAN_NET_STREAM connectWithServer:@"192.168.10.10" port:9716 delegate:nil];
}

- (IBAction)send:(id)sender
{    
    [DATA_SOURCE login];
    [IvanTEST TEST_BEGIN_Enable:NO];
    [IvanNetStreamTEST TEST_beginEnable:NO];
    [GzipTEST TEST_beginEnable:NO];
    [AttachmentTEST TEST_beginEnable:NO];
    [IvanSqliteDBTEST TEST_beginEnable:NO];
}
@end